<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Class	: M_po
 * 
 * Table	: po
 *  
 * @author Eko Junaidi Salam
 *
 */
class M_po extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	/**
	 * Fungsi	: getAll
	 * 
	 * Untuk mengambil all-data
	 * 
	 * @param number $start
	 * @param number $page
	 * @param number $limit
	 * @return json
	 */
	function getPendingOrder($start, $page, $limit){
		$sql = "SELECT t1.NOMOR_LPB,pod.NOMOR_PO,pod.NAMA_BARANG,pod.JUMLAH as POD_QTY,
				IF(t1.QUANTITY IS null,0,t1.QUANTITY) as TBD_QTY,
				pod.JUMLAH-IF(NOT ISNULL(t1.QUANTITY),t1.QUANTITY,0) as QUANTITY ,
				pod.HARGA_SATUAN
				FROM podetail pod
				LEFT JOIN (
					SELECT tbd.NOMOR_LPB,tbd.NOMOR_PO,tbd.NAMA_BARANG,sum(tbd.QUANTITY) as QUANTITY
					FROM terimabarangdet tbd
					GROUP BY tbd.NAMA_BARANG
				) as t1
				ON t1.NOMOR_PO=pod.NOMOR_PO AND t1.NAMA_BARANG=pod.NAMA_BARANG
				WHERE pod.JUMLAH <> t1.QUANTITY or ISNULL(t1.QUANTITY)";
				
				$query = $this->db->query($sql)->result();
				$total = $this->db->query($sql)->num_rows();
		
		$data   = array();
		foreach($query as $result){
			$data[] = $result;
		}
		
		$json	= array(
						'success'   => TRUE,
						'message'   => "Loaded data",
						'total'     => $total,
						'data'      => $data
		);
		
		return $json;
	}
	
	function getAll($filters,$start, $page, $limit){
		if($filters == null){
			$query  = $this->db->limit($limit, $start)->order_by('ID', 'DESC')->get('po')->result();
			$total  = $this->db->get('po')->num_rows();
			
			$data   = array();
			foreach($query as $result){
				$data[] = $result;
			}
			
			$json	= array(
							'success'   => TRUE,
							'message'   => "Loaded data",
							'total'     => $total,
							'data'      => $data
			);
			
			return $json;
		}
		else
		{
			// GridFilters sends filters as an Array if not json encoded
			if (is_array($filters)) {
				$encoded = false;
			} else {
				$encoded = true;
				$filters = json_decode($filters);
			}

			$where = " 0=0 ";
			$qs = '';

			// loop through filters sent by client
			if (is_array($filters)) {
				for ($i=0;$i<count($filters);$i++){
					$filter = $filters[$i];
					// echo $this->firephp->info($filter);
					// assign filter data (location depends if encoded or not)
					if ($encoded) {
						$field = $filter->field;
						$value = $filter->value;
						$compare = isset($filter->comparison) ? $filter->comparison : null;
						$filterType = $filter->type;
					} else {
						$field = $filter['field'];
						$value = $filter['data']['value'];
						$compare = isset($filter['data']['comparison']) ? $filter['data']['comparison'] : null;
						$filterType = $filter['data']['type'];
					}

					switch($filterType){
						case 'string' : $qs .= " AND ".$field." LIKE '%".$value."%'"; Break;
						case 'list' :
							if (strstr($value,',')){
								$fi = explode(',',$value);
								for ($q=0;$q<count($fi);$q++){
									$fi[$q] = "'".$fi[$q]."'";
								}
								$value = implode(',',$fi);
								$qs .= " AND ".$field." IN (".$value.")";
							}else{
								$qs .= " AND ".$field." = '".$value."'";
							}
						Break;
						case 'boolean' : $qs .= " AND ".$field." = ".($value); Break;
						case 'numeric' :
							switch ($compare) {
								case 'eq' : $qs .= " AND ".$field." = ".$value; Break;
								case 'lt' : $qs .= " AND ".$field." < ".$value; Break;
								case 'gt' : $qs .= " AND ".$field." > ".$value; Break;
							}
						Break;
						case 'date' :
							switch ($compare) {
								case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d',strtotime($value))."'"; Break;
								case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d',strtotime($value))."'"; Break;
								case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d',strtotime($value))."'"; Break;
							}
						Break;
						case 'datetime' :
							switch ($compare) {
								case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d H:i:s',strtotime($value))."'"; Break;
								case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d H:i:s',strtotime($value))."'"; Break;
								case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d H:i:s',strtotime($value))."'"; Break;
							}
						Break;
					}
				}
				$where .= $qs;
			}
			
			$sql = "SELECT * FROM PO
			WHERE ".$where;
			$sql .= " ORDER BY ID ASC";
			$sql .= " LIMIT ".$start.",".$limit;
			$query = $this->db->query($sql)->result();
			
			$total  = $this->db->query("SELECT count(ID) as total
			FROM PO
			WHERE ".$where)->result();
			
			$data   = array();
			foreach($query as $result){
				$data[] = $result;
			}
			
			$json	= array(
				'success'   => TRUE,
				'message'   => "Loaded data",
				'total'     => $total[0]->total,
				'data'      => $data
			);
			
			return $json;
		}
	}
	
	/**
	 * Fungsi	: save
	 * 
	 * Untuk menambah data baru atau mengubah data lama
	 * 
	 * @param array $data
	 * @return json
	 */
	function save($data){
		$last   = NULL;
		
		$pkey = array('NOMOR_PO'=>$data->NOMOR_PO);
		
		if($this->db->get_where('po', $pkey)->num_rows() > 0){
			/*
			 * Data Exist
			 */			 
			
			 
			$arrdatau = array('TANGGAL_PO'=>(strlen(trim($data->TANGGAL_PO)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_PO)) : NULL),'NAMA_SUPPLIER'=>$data->NAMA_SUPPLIER,'TARGET_BRG_DATANG'=>(strlen(trim($data->TARGET_BRG_DATANG)) > 0 ? date('Y-m-d', strtotime($data->TARGET_BRG_DATANG)) : NULL),'LOKAL_IMPORT'=>$data->LOKAL_IMPORT,'BANKER'=>$data->BANKER,'IMPORTER'=>$data->IMPORTER,'ALAMAT_IMPORTER'=>$data->ALAMAT_IMPORTER,'PACKING'=>$data->PACKING,'SHIPMENT'=>$data->SHIPMENT,'LOADING_PORT'=>$data->LOADING_PORT,'DEST_PORT'=>$data->DEST_PORT,'OTHER_COND'=>$data->OTHER_COND,'CURRENCY'=>$data->CURRENCY,'KURS'=>$data->KURS,'KETERANGAN'=>$data->KETERANGAN,'DIPESAN_OLEH'=>$data->DIPESAN_OLEH,'DISETUJUI_OLEH'=>$data->DISETUJUI_OLEH,'FLAG'=>$data->FLAG);
			 
			$this->db->where($pkey)->update('po', $arrdatau);
			$last   = $data;
			
		}else{
			/*
			 * Data Not Exist
			 * 
			 * Process Insert
			 */
			
			$jenis = ($data->LOKAL_IMPORT == 'LOKAL'?'polokal':'poimport');
			
			$kode = $this->m_public_function->GenCode($jenis);
			
			$arrdatac = array('NOMOR_PO'=>$kode->NOMOR_PO,'TANGGAL_PO'=>(strlen(trim($data->TANGGAL_PO)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_PO)) : NULL),'NAMA_SUPPLIER'=>$data->NAMA_SUPPLIER,'TARGET_BRG_DATANG'=>(strlen(trim($data->TARGET_BRG_DATANG)) > 0 ? date('Y-m-d', strtotime($data->TARGET_BRG_DATANG)) : NULL),'LOKAL_IMPORT'=>$data->LOKAL_IMPORT,'BANKER'=>$data->BANKER,'IMPORTER'=>$data->IMPORTER,'ALAMAT_IMPORTER'=>$data->ALAMAT_IMPORTER,'PACKING'=>$data->PACKING,'SHIPMENT'=>$data->SHIPMENT,'LOADING_PORT'=>$data->LOADING_PORT,'DEST_PORT'=>$data->DEST_PORT,'OTHER_COND'=>$data->OTHER_COND,'CURRENCY'=>$data->CURRENCY,'KURS'=>$data->KURS,'KETERANGAN'=>$data->KETERANGAN,'DIPESAN_OLEH'=>$data->DIPESAN_OLEH,'DISETUJUI_OLEH'=>$data->DISETUJUI_OLEH,'ID'=>$data->ID,'FLAG'=>$data->FLAG);
			 
			$this->db->insert('po', $arrdatac);
			$last   = $this->db->where($pkey)->get('po')->row();
			
			$rs = $this->db->where(array('param' => ($data->LOKAL_IMPORT == 'LOKAL'?'POL':'POI')))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$this->db->where(array('param' => ($data->LOKAL_IMPORT == 'LOKAL'?'POL':'POI')))->update('init', array('value' => $no));
		}
		
		$total  = $this->db->get('po')->num_rows();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil disimpan',
						'total'     => $total,
						"data"      => $last
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: delete
	 * 
	 * Untuk menghapus satu data
	 * 
	 * @param array $data
	 * @return json
	 */
	function delete($data){
		$pkey = array('NOMOR_PO'=>$data->NOMOR_PO);
		
		$this->db->where($pkey)->delete('po');
		
		$total  = $this->db->get('po')->num_rows();
		$last = $this->db->get('po')->result();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil dihapus',
						'total'     => $total,
						"data"      => $last
		);				
		return $json;
	}
}
?>